<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>登录</title>
    <meta name="keywords" content="">
    <meta name="description" content="">

    <link rel="shortcut icon" href="favicon.ico">
    <link href="css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="css/font-awesome.css?v=4.4.0" rel="stylesheet">

    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css?v=4.1.0" rel="stylesheet">
    <!--[if lt IE 9]>
    <meta http-equiv="refresh" content="0;ie.html"/>
    <![endif]-->
    <script>
        if (window.top !== window.self) {
            window.top.location = window.location;
        }
    </script>

    <style type="text/css">
        .login-fail {
            color: red;
        }
    </style>
</head>

<body class="gray-bg">

<div class="middle-box text-center loginscreen  animated fadeInDown">
    <div style="margin-top: 100px;">
        <div>
            <!-- <h1 class="logo-name">h</h1> -->
        </div>
        <h3>欢迎使用工作流系统</h3>

        <div class="form-group">
            <input type="text" name="userName" class="form-control" placeholder="用户名" required="">
        </div>
        <div class="form-group">
            <input type="password" name="pwd" class="form-control" placeholder="密码" required="">
        </div>
        <button type="submit" class="btn btn-primary block full-width m-b" id="login"> 登 录</button>
        <p class="text-muted text-center">
            <span class="login-fail" id="msg"></span>
        </p>
    </div>
</div>

<!-- 全局js -->
<script src="js/jquery.min.js?v=2.1.4"></script>
<script src="js/bootstrap.min.js?v=3.3.6"></script>

<script type="text/javascript">

    (function ($) {
        $.getUrlParam = function (name) {
            let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            let r = window.location.search.substr(1).match(reg);
            if (r != null) return decodeURI(r[2]);
            return null;
        }
    })(jQuery);

    $(function () {

        //session过期
        let url = window.location.href;
        if (!$.isEmptyObject(url) && url.indexOf('sexpire') !== -1) {
            $('#msg').text('会话过期，请重新登录。');
        }

        //登录失败
        let errorType = $.getUrlParam('errorType');
        if (errorType === 1 || errorType === '1') {
            $('#msg').text('cas没有回传用户数据，请联系管理员。');
        }

        if (errorType === 2 || errorType === '2') {
            //通过cas回传的用户名/工号["+ loginName +"]没有查询到用户数据。
            $('#msg').text('无权访问此服务。');
        }

        //监听回车按下事件
        document.onkeydown = function(e){
            var ev = document.all ? window.event : e;
            if(ev.keyCode === 13) {
                $('#login').click();
            }
        }

        //登录
        $('#login').click(function () {
            const userName = $('input[name="userName"]').val();
            const pwd = $('input[name="pwd"]').val();

            if ($.isEmptyObject(userName)) {
                $('#msg').text('请填写用户名');
                return;
            }
            if ($.isEmptyObject(pwd)) {
                $('#msg').text('请填写密码');
                return;
            }

            //登录
            $.ajax({
                type: "POST",
                url: "/login/local",
                contentType: "application/x-www-form-urlencoded",
                dataType: "json",
                data: {
                    "userName": userName,
                    "pwd": pwd
                },
                success: function (data) {
                    if (data.code === 0) {
                        //跳转到系统首页
                        window.location.href = "/admin/index.html";
                    } else {
                        console.log(JSON.stringify(data.payload) + '--登录失败');
                        $('#msg').text(data.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(textStatus);
                    $('#msg').text('登录错误');
                }
            });
        });
    })
</script>


</body>

</html>
